#include "Die.h"
#include "qpainter.h"
#include <QGlobal.h>
#include <qevent.h>
#include <QTime>

Die::Die(QWidget * parent) : QWidget(parent),
value(0)
,mImage(0)
{
	mImage = new QImage();
	mImage->load("Images\\dice.png");
	QTime time = QTime::currentTime();
	qsrand((uint)time.msec());
	setFixedSize(64,64);
	roll();
}

Die::~Die()
{
	if(mImage != 0)
		delete mImage;
}

void Die::roll(void)
{
	value = qrand() % 6 + 1;
}

void Die::paintEvent(QPaintEvent * event)
{
	QPainter qpainter(this);
	qpainter.drawImage(0,0,*mImage,(value % 6) * 64, (value - 1) * 64, 64,64); 
}

 void Die::mousePressEvent(QMouseEvent *event)
 {
	 // For easier demo-int
     if (event->button() == Qt::LeftButton) 
	 {
		 if(++value > 6)
			 value = 1;
     }
	 update();
 }